package com.lhn.netty.nio;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author lhn
 * @date 2025/05/26 17:00
 * @since 1.0.0
 **/
public class NIOFileChannel03 {

    public static void main(String[] args) throws Exception {
        FileInputStream fileInputStream = new FileInputStream("1.txt");
        FileChannel channel01 = fileInputStream.getChannel();

        FileOutputStream  fileOutputStream = new FileOutputStream("2.txt");
        FileChannel channel02 = fileOutputStream.getChannel();

        ByteBuffer buffer = ByteBuffer.allocate(5);
        while (true) {
            buffer.clear();
            int read = channel01.read(buffer);
            // 读取完毕
            if (read == -1) {
                break;
            }
            // 将读到的数据写入到 channel02
            buffer.flip();
            int write = channel02.write(buffer);
        }
        fileInputStream.close();
        fileOutputStream.close();
    }

}